home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / scripts / cron.tcl < prev    next >
Encoding:
Text File  |  1993-11-19  |  3.9 KB  |  148 lines  |  [TEXT/$Tcl]

  1.  
  2.  
  3. set env(CRON_EXPR) { run_cron }
  4. set env(CRON_TICKS) 3600
  5.  
  6. #
  7. # set cron(NAME) "month(1-12) domon(1-31) dowk(1,Mon-7,Sun) hour(1-23) minute(1-59) expr"
  8. #
  9. set cron(1) { * * * * 1-60 { feedback "CRON TASK NUMBER ONE!!!!!!" } }
  10. set cron(2) { * * * * 5-30,35,40 { feedback "CRON TASK NUMBER TWO!!!!!!" } }
  11. set cron(3) { 4-6 * * * 1-60 { feedback "CRON TASK NUMBER THREE!!!!!!" } }
  12. set cron(4) { * 4 * * 1-60 { feedback "CRON TASK NUMBER FOUR!!!!!!" } }
  13. set cron(5) { * 1-7 * * 1-60 { feedback "CRON TASK NUMBER FIVE!!!!!!" } }
  14. set cron(6) { * * 6 * 1-60 { feedback "CRON TASK NUMBER SIX!!!!!!" } }
  15.  
  16. set MONTHS(Jan) 1
  17. set MONTHS(Feb) 2
  18. set MONTHS(Mar) 3
  19. set MONTHS(Apr) 4
  20. set MONTHS(May) 5
  21. set MONTHS(Jun) 6
  22. set MONTHS(Jul) 7
  23. set MONTHS(Aug) 8
  24. set MONTHS(Sep) 9
  25. set MONTHS(Oct) 10
  26. set MONTHS(Nov) 11
  27. set MONTHS(Dec) 12
  28.  
  29. set DOWS(Mon) 1
  30. set DOWS(Tue) 2
  31. set DOWS(Wed) 3
  32. set DOWS(Thu) 4
  33. set DOWS(Fri) 5
  34. set DOWS(Sat) 6
  35. set DOWS(Sun) 7
  36.  
  37. proc check_cron_item { list theitem } {
  38.  
  39.     set RESULT 0
  40.  
  41.     foreach mine [split $list ,] {
  42.         #puts stdout "CHECK: '$theitem' against '$mine'"
  43.         if "! [string compare $mine $theitem]" then {
  44.             #puts stdout "CHECK: SUCCESS!!!! on '$mine'"
  45.             set RESULT 1
  46.             break
  47.         } else {
  48.             set DL [split "$mine" -]
  49.             if "[llength $DL] == 2" then {
  50.                 #puts stdout "CHECK: RANGE: [lindex $DL 0] <= $theitem <= [lindex $DL 1]"
  51.                 if "$theitem >= [lindex $DL 0] && $theitem <= [lindex $DL 1]" then {
  52.                     #puts stdout "CHECK: RANGE SUCCESS!!!! on '$mine'"
  53.                     set RESULT 1
  54.                     break
  55.                 }
  56.             }
  57.         }
  58.     }
  59.  
  60.     return $RESULT
  61. }
  62.  
  63.  
  64. proc run_cron { } {
  65.     global cron MONTHS DOWS
  66.  
  67.     #puts stdout "NOW: [mtime [now] abbrev]"
  68.     set thetime [mtime [now] abbrev]
  69.     set thedow $DOWS([string trimright [lindex [lindex $thetime 0] 0] ,])
  70.     set themon $MONTHS([lindex [lindex $thetime 0] 1])
  71.     set thedom [string trimright [lindex [lindex $thetime 0] 2] ,]
  72.     set theyear [lindex [lindex $thetime 0] 3]
  73.     set thehour [lindex [split [lindex [lindex $thetime 1] 0] :] 0]
  74.     set themin [lindex [split [lindex [lindex $thetime 1] 0] :] 1]
  75.     set thesec [lindex [split [lindex [lindex $thetime 1] 0] :] 2]
  76.     set theampm [lindex [lindex $thetime 1] 1]
  77.  
  78.     if "! [string compare PM $theampm]" then {
  79.         if "$thehour < 12" then {
  80.             set thehour [expr "$thehour + 12"]
  81.             }
  82.         }
  83.  
  84.     # puts stdout "$thedow $themon $thedom, $theyear $thehour:$themin:$thesec"
  85.  
  86.     foreach index [lsort [array names cron]] {
  87.         #puts stdout "Checking cron task cron($index)";
  88.         #puts stdout "## $index ## $cron($index)";
  89.         set mon [lindex $cron($index) 0]
  90.         set dom [lindex $cron($index) 1]
  91.         set dow [lindex $cron($index) 2]
  92.         set hour [lindex $cron($index) 3]
  93.         set min [lindex $cron($index) 4]
  94.         set task [lindex $cron($index) 5]
  95.  
  96.         #puts stdout " --> Mon $mon Dom $dom Dow $dow Hour $hour Min $min";
  97.         #puts stdout " --> Task $task";
  98.  
  99.         if "[string compare * $mon]" then {
  100.             #puts stdout "Have Month spec, checking '$mon' against '$themon'"
  101.             if "! [check_cron_item $mon $themon]" then {
  102.                 #puts stdout " --> MONTH FAILED."
  103.                 continue;
  104.                 }
  105.             }
  106.  
  107.         if "[string compare * $dom]" then {
  108.             #puts stdout "Have day of month spec, checking '$dom' against '$thedom'"
  109.             if "! [check_cron_item $dom $thedom]" then {
  110.                 #puts stdout " --> DAY OF MONTH FAILED"
  111.                 continue;
  112.                 }
  113.             }
  114.  
  115.         if "[string compare * $dow]" then {
  116.             #puts stdout "Have day of week spec, checking '$dow' against '$thedow'"
  117.             if "! [check_cron_item $dow $thedow]" then {
  118.                 #puts stdout " --> DAY OF WEEK FAILED"
  119.                 continue;
  120.                 }
  121.             }
  122.  
  123.         if "[string compare * $hour]" then {
  124.             #puts stdout "Have hour spec, checking '$hour' against '$thehour'"
  125.             if "! [check_cron_item $hour $thehour]" then {
  126.                 #puts stdout " --> HOUR FAILED"
  127.                 continue;
  128.                 }
  129.             }
  130.  
  131.         if "[string compare * $min]" then {
  132.             #puts stdout "Have minute spec, checking '$min' against '$themin'"
  133.             if "! [check_cron_item $min $themin]" then {
  134.                 #puts stdout " --> MINUTE FAILED"
  135.                 continue;
  136.                 }
  137.             }
  138.  
  139.         #feedback "CRON: evaluating '$task'"
  140.         catch "$task"
  141.         }
  142.  
  143.     }
  144.  
  145. #run_cron
  146.  
  147.  
  148.